package com.shop.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.shop.entity.Address;
import com.shop.shop.mapper.AddressMapper;
import com.shop.shop.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shop.utile.MapUtile;
import com.shop.shop.utile.PageUtile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 地址表 服务实现类
 * </p>
 *
 * @author 小🌤
 * @since 2021-04-22
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
  @Autowired
  private AddressMapper addressMapper;

    @Override
    public Map<String, Object> findByPage(MapUtile map) {
        Map<String, Object> masp=new HashMap<>();
        Page<Address> page = new PageUtile<Address>().getPage(map);
        QueryWrapper<Address> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("as_default");
        baseMapper.selectPage(page,wrapper);
        List<Address> list = page.getRecords();
        masp.put("list", list);
        masp.put("pages", page.getPages());
        masp.put("size", page.getSize());
        masp.put("page", page.getCurrent());
        masp.put("total", page.getTotal());
        return masp;
    }

    @Override
    public boolean updateIdUser(Address address, String id) {
        Address address1 = new Address();
        address1.setUrId(Integer.parseInt(id));
        address1.setAsDefault(0);
        addressMapper.updateDefalutAddress(address1);

        int i = baseMapper.updateById(address);

        return i>0;
    }

    @Override
    public Address findByIDDF(int id) {
        QueryWrapper<Address> addressQueryWrapper=new QueryWrapper<>();
        addressQueryWrapper.eq("ur_id",id);
        addressQueryWrapper.eq("as_default",1);
        Address address = getOne(addressQueryWrapper);
        return address;
    }
}
